home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / PredatorPrey / Miscellany.c < prev    next >
Text File  |  1996-06-22  |  8KB  |  332 lines

  1. /* Miscellany.c */    /*    C14 Calculator */
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>            /* for ok */
  7. #include <Events.h>
  8. /*#include <Lists.h>*/
  9. #include <Menus.h>
  10. #include <TextEdit.h>
  11. #include "Globals.h"
  12. #include "ResourceDefs.h"    
  13.  
  14. #include "Miscellany.h"
  15.  
  16. #include <Errors.h>
  17. #include <Packages.h>
  18. #include <Resources.h>
  19. #include <Script.h>                /* for GetMBarHeight */
  20. #include <Sound.h>
  21.  
  22. #pragma segment Miscellany
  23.  
  24. void    MiscellanySeg()    {}
  25.     
  26. /* global vars: */
  27. Boolean            errorFlag;    
  28.  
  29. #define createTop        75                                   
  30. #define createLeft        100
  31. #define topLeft(r)    (((Point *) &(r))[0])
  32. #define botRight(r)    (((Point *) &(r))[1])
  33.  
  34. /*----------*/
  35. void    Acknowledge        (short            alertID)
  36. {
  37.     short            itemHit;
  38.     GrafPtr            savePort;
  39.  
  40.     GetPort (&savePort);
  41.     InitCursor ();
  42.     itemHit = StopAlert (alertID, NULL);
  43.     SetPort (savePort);
  44. } /*Acknowledge*/
  45.  
  46. /*----------*/
  47. Boolean    Confirm        (short            alertID)
  48. {
  49.     Boolean            result;
  50.     GrafPtr            savePort;
  51.  
  52.     GetPort (&savePort);
  53.     InitCursor ();
  54.     result = (CautionAlert (alertID, NULL) == ok);
  55.     SetPort (savePort);
  56.     return (result);
  57. } /*Confirm*/
  58.  
  59. /*----------*/
  60. Boolean GetErrorMessage    (OSErr        resultCode,
  61.                          Str255        message);
  62. Boolean GetErrorMessage (OSErr        resultCode,
  63.                          Str255        message)
  64. {
  65.     StringHandle    msgHndl;
  66.  
  67.     msgHndl = (StringHandle) GetResource ('ErMs', -resultCode);
  68.     if (msgHndl != NULL) {
  69.         BlockMove (&(**msgHndl), message, 256);
  70.         return (true);
  71.     } else {
  72.         message [0] = 0;
  73.         return (false);
  74.     }
  75. } /*GetErrorMessage*/
  76.  
  77. /*----------*/
  78. Boolean    CheckOS        (OSErr            resultCode)
  79. {
  80.     Str255            message;
  81.     Str255            errNum;
  82.  
  83.     if (resultCode == noErr) {
  84.         return (true);
  85.     } else {
  86.         if (GetErrorMessage (resultCode, message)) {
  87.             ParamText (message, "\p", "\p", "\p");
  88.         } else { /*generic message*/
  89.             if (!GetErrorMessage (0, message)) {
  90.                 BlockMove ("\pOS Error ", message, 10);
  91.             }
  92.             NumToString (resultCode, errNum);
  93.             ParamText (message, errNum, "\p", "\p");
  94.         }
  95.         Acknowledge (IOErrorID);
  96.         errorFlag = true;
  97.         return (false);
  98.     }
  99. } /*CheckOS*/
  100.  
  101. #ifndef THINK_C            /*MPW C*/
  102. static StringPtr failNilRsrcText = {"\pSorry, a resource needed by this application cannot be found. Click the mouse or press any key to quit."};
  103. #else                    /*THINK C*/
  104. static StringPtr failNilRsrcText = {"\pSorry, a resource needed by this application cannot be found. THINK C requires that your resource file (AppMaker document) be in the same folder and have the same name as the project file with \".rsrc\" appended. Click the mouse or press any key to quit."};
  105. #endif
  106.  
  107. /*----------*/
  108. /* If resource = nil, this procedure puts up a window reporting that a resource */
  109. /* could not be found, then quits the program. */
  110. /*----------*/
  111. void    FailNilResource    (Handle            resource)
  112. {
  113.     Rect            windowBounds;        /* global coordinates */
  114.     WindowPtr        window;
  115.     Rect            textBounds;            /* local coordinates */
  116.  
  117.     if (resource == nil) {
  118.         SetRect (&windowBounds, 90 /*left*/, 70 /*top*/, 420 /*right*/, 200 /*bottom*/);
  119.         window = NewWindow (nil /*wStorage*/, &windowBounds, "\p" /*title*/, true /*visFlag*/,
  120.                             dBoxProc /*wDefProcID*/, (WindowPtr)-1 /*behind*/,
  121.                             false /*goAwayFlag*/, 0L /*refCon*/);
  122.         if (window != nil) {
  123.             SetPort (window);
  124.             InitCursor ();
  125.             SetRect (&textBounds, 20 /*left*/, 20 /*top*/, 320 /*right*/, 120 /*bottom*/);
  126.             TextBox (&(failNilRsrcText [1]), failNilRsrcText [0], &textBounds, teJustLeft);
  127.             while (!GetNextEvent (mDownMask + keyDownMask, &curEvent))
  128.                 ;            /*wait for the next mouseDown or keyDown event*/
  129.             DisposeWindow (window);
  130.         } else {                /*window == nil, NewWindow must have failed*/
  131.             SysBeep (1);
  132.         }
  133.         ExitToShell ();
  134.     } /* otherwise, resource != nil, do nothing */
  135. } /*FailNilResource*/
  136.  
  137. /*----------*/
  138. Boolean    FileExists    (Str255            fName,
  139.                      short            vRefNum)
  140. {
  141.     FInfo            fileInfo;                               
  142.  
  143.     return (GetFInfo (fName, vRefNum, &fileInfo) == noErr);
  144. } /*FileExists*/
  145.  
  146. /*----------*/
  147. Boolean    CreateFile    (SFReply        *sfInfo,
  148.                      Str255            prompt,
  149.                      Str255            suggestion,
  150.                      OSType            creator,
  151.                      OSType            fileType)
  152. {
  153.      Point            dlgOrigin;                               
  154.     Boolean            okay;
  155.     
  156.     SetPt     (&dlgOrigin, createLeft, createTop);
  157.     SFPutFile (dlgOrigin, prompt, suggestion, NULL, sfInfo);
  158.     okay = sfInfo->good;
  159.     if (okay) {
  160.         if (FileExists (sfInfo->fName, sfInfo->vRefNum)) {
  161.             okay = CheckOS (FSDelete (sfInfo->fName, sfInfo->vRefNum));
  162.         }
  163.     }
  164.     if (okay) {
  165.         okay = CheckOS (Create (sfInfo->fName, sfInfo->vRefNum, creator, fileType));
  166.     }
  167.     return (okay);
  168. } /*CreateFile*/
  169.  
  170. /*----------*/
  171. void    ScaleWindow        (WindowPtr        window,
  172.                          Boolean        scaleSize)
  173. {
  174.     Rect            newBounds;
  175.     Rect            origScreen;
  176.     Rect            curScreen;
  177.     short            height;
  178.     short            width;
  179.  
  180.     newBounds = window->portRect;
  181.     LocalToGlobal (&topLeft (newBounds)); 
  182.     LocalToGlobal (&botRight (newBounds));
  183.     SetRect (&origScreen, 0, 20, 512, 342);
  184.     curScreen = qd.screenBits.bounds;
  185.     curScreen.top = GetMBarHeight ();
  186.     height = newBounds.bottom - newBounds.top;
  187.     width  = newBounds.right  - newBounds.left;
  188.     MapRect (&newBounds, &origScreen, &curScreen);
  189.     if (scaleSize) {
  190.     SizeWindow (window, newBounds.right  - newBounds.left,
  191.                         newBounds.bottom - newBounds.top, true);
  192.     } else {
  193.         newBounds.top  = newBounds.bottom - height;
  194.         newBounds.left = newBounds.right  - width;
  195.     }
  196.     MoveWindow (window, newBounds.left, newBounds.top, false);
  197. } /*ScaleWindow*/
  198.  
  199. /*----------*/
  200. void    DrawClippedGrow (short            x,
  201.                          short            y)
  202. {
  203.     RgnHandle        saveClip;
  204.     Rect            growRect;
  205.     Rect            portRect;
  206.  
  207.     saveClip = NewRgn ();
  208.     GetClip (saveClip);
  209.     portRect = qd.thePort->portRect;
  210.     if (x >= 0) {
  211.         x = portRect.left + x;
  212.     } else {
  213.         x = portRect.right + x;
  214.     }
  215.     if (y >= 0) {
  216.         y = portRect.top + y;
  217.     } else {
  218.         y = portRect.bottom + y;
  219.     }
  220.     SetRect (&growRect, x, y, portRect.right, portRect.bottom);
  221.     ClipRect (&growRect);
  222.     DrawGrowIcon (qd.thePort);
  223.     SetClip (saveClip);
  224.     DisposeRgn (saveClip);
  225. } /*DrawClippedGrow*/
  226.  
  227. /*----------*/
  228. void    DoRadioMenu        (MenuHandle        menu,
  229.                          short            firstItem,
  230.                          short            lastItem,
  231.                          short            itemNr)
  232. {
  233.     short            i;
  234.     
  235.     for (i = firstItem; i <= lastItem; i++) {
  236.         CheckItem (menu, i, (i == itemNr));
  237.     }
  238. } /*DoRadioMenu*/
  239.  
  240. /*----------*/
  241. void    PlaySound        (MenuHandle        soundsMenu,
  242.                          short            itemNr)
  243. {
  244.     Str255            soundName;
  245. /* cw5 */    //SndListHandle    sound;
  246.     OSErr            errCode;
  247.  
  248.     GetItem (soundsMenu, itemNr, soundName);
  249.     /* cw5 */
  250.     //sound = GetNamedResource ('snd ', soundName);
  251.     //if (sound != NULL) {
  252.         //errCode = SndPlay (NULL, sound, false);
  253.     //}
  254. } /*PlaySound*/
  255.  
  256. /*----------*/
  257. /* The following routines implement a linked list of Items.*/
  258. /* These routines expect that the first field of every item*/
  259. /* is a pointer (Handle) to the next item. They make no*/
  260. /* assumptions about the remainder of each item.*/
  261.  
  262. /* The implementation happens to be a singly linked list.*/
  263. /* It is linked circularly with the last item pointing*/
  264. /* to the first item. The listHead points to the*/
  265. /* last item. This implementation provides fast*/
  266. /* access to both the head and tail of the list*/
  267. /* without the extra space of a doubly linked list.*/
  268.  
  269. /*----------*/
  270. void    LinkLast    (Handle        *listHead,
  271.                      Handle        newItem)
  272. {
  273.     LinkedItem        item;
  274.     LinkedItem        last;
  275.  
  276.     item = (LinkedItem) newItem;
  277.     if (*listHead == NULL) {
  278.         (**item).next = item;
  279.     } else {
  280.         last = (LinkedItem) *listHead;
  281.         (**item).next = (**last).next;
  282.         (**last).next = item;
  283.     }
  284.     *listHead = (Handle) item;
  285. } /*LinkLast*/
  286.  
  287. /*----------*/
  288. Handle    GetNth        (Handle        listHead,
  289.                      short        num)
  290. {
  291.     LinkedItem            item;
  292.     short                I;
  293.  
  294.     item = NULL;
  295.     if (listHead != NULL) {
  296.         item = (LinkedItem) listHead;
  297.         for (I = 1; I <= num; I++) {
  298.             item = (**item).next;
  299.         } /*for*/
  300.     }
  301.     return ((Handle) item);
  302. } /*GetNth*/
  303.  
  304. /*----------*/
  305. Handle    UnlinkNth    (Handle        *listHead,
  306.                      short        num)
  307. {
  308.     LinkedItem            item;
  309.     LinkedItem            prev;
  310.     short                I;
  311.  
  312.     item = NULL;
  313.     if (*listHead != NULL) {
  314.         prev = (LinkedItem) *listHead;
  315.         for (I = 1; I <= num - 1; I++) {
  316.             prev = (**prev).next;
  317.         } /*for*/
  318.         item = (**prev).next;
  319.         if (item == prev) {
  320.             *listHead = NULL;
  321.         } else {
  322.             (**prev).next = (**item).next;
  323.             if (*listHead == (Handle) item) {
  324.                 *listHead = (Handle) prev;
  325.             }
  326.         }
  327.     }
  328.     return ((Handle) item);
  329. } /*UnlinkNth*/
  330.  
  331. /* Miscellany */
  332.